/*
 * Created on 2005/05/10
 * Author aki@www.xucker.jpn.org
 * License Apache2.0 or Common Public License
 */
package jp.sourceforge.akjrcp.ui.draw2d;

import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.FigureListener;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ScalableLayeredPane;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;



/**
 * 
 *
 */
public class IndepentScalebleLayerdPaneCanvas extends FigureCanvas{
    ScalableLayeredPane scalableLayeredPane;
    Dimension prevDimension;
    
    public double getHorizontalScale() {
        return horizontalScale;
    }
    public void setHorizontalScale(double horizontalScale) {
        this.horizontalScale = horizontalScale;
    }
    public ScalableLayeredPane getScalableLayeredPane() {
        return scalableLayeredPane;
    }
    public void setScalableLayeredPane(ScalableLayeredPane scalableLayeredPane) {
        this.scalableLayeredPane = scalableLayeredPane;
    }
    public double getVerticalScale() {
        return verticalScale;
    }
    public void setVerticalScale(double verticalScale) {
        this.verticalScale = verticalScale;
    }
    IndepentScalebleLayerdPaneCanvas lws;
    private double horizontalScale=1;
    private double verticalScale=1;
    private boolean baseHorizontal=true;
    
    
    public boolean isBaseHorizontal() {
        return baseHorizontal;
    }
    public void setBaseHorizontal(boolean baseHorizontal) {
        this.baseHorizontal = baseHorizontal;
    }
    /**
     * @param parent
     */
    public IndepentScalebleLayerdPaneCanvas(Composite parent) {
        super(parent);
        lws=this;
        this.setLayout(new FillLayout());
        // TODO Auto-generated constructor stub
        scalableLayeredPane = new ScalableLayeredPane();
        
        scalableLayeredPane.addFigureListener(new Resized());
        
        this.setContents(scalableLayeredPane);
    }
    
    public void setScale(double newW,double newH){
        double wchanged=newW/horizontalScale;
	    double hchanged=newH/verticalScale;
	   // log.info("hzoom="+verticalScale+",newH="+newH+",hchanged="+hchanged);
	    int scrollX=lws.getHorizontalBar().getSelection();
	    int scrollY=lws.getVerticalBar().getSelection();
	    //log.info("y-max:"+lws.getVerticalBar().getMaximum());
	  //  log.info(scalableLayeredPane.getSize());
	    
	    
	    IFigure[] figures=null;
	    synchronized(this){
	        figures=(IFigure[]) scalableLayeredPane.getChildren().toArray(new IFigure[scalableLayeredPane.getChildren().size()]);
	    }
	    
	    
	    prevDimension=scalableLayeredPane.getSize();
	    if(baseHorizontal){
	    double inW=newW/newH;
	    for (int i = 0; i < figures.length; i++) {
	        if(figures[i] instanceof DoubleScalableFigure){
            ((DoubleScalableFigure)figures[i]).setScale(inW,1);
	        }
        }
	    scalableLayeredPane.setScale(newH);
	    }else{
	        double inH=newH/newW;
		    for (int i = 0; i < figures.length; i++) {
		        if(figures[i] instanceof DoubleScalableFigure){
	            ((DoubleScalableFigure)figures[i]).setScale(1,inH);
		        }
	        }
		    scalableLayeredPane.setScale(newW);
	    }
	   
	    
	    
	   
	    horizontalScale=newW;
	    verticalScale=newH;
	    //log.info("y-max-resized:"+lws.getVerticalBar().getMaximum());
	 //   log.info(scalableLayeredPane.getSize());
	    
	    scalableLayeredPane.revalidate();
	    this.layout();
    }
    
    
    public class Resized implements FigureListener{

        


         /* (non-Javadoc)
          * @see org.eclipse.draw2d.FigureListener#figureMoved(org.eclipse.draw2d.IFigure)
          */
         public void figureMoved(IFigure source) {
             if(!source.getSize().equals(prevDimension)){
             //log.info("moved:"+source.getSize());
             int scrollX=lws.getHorizontalBar().getSelection();
     	    int scrollY=lws.getVerticalBar().getSelection();
     	    //log.info("canvas:"+lws.getClientArea());
     	    //scroll move.
     	    //log.info("scrollX:"+scrollX);
     	    //log.info("scrollY:"+scrollY);
     	   
     	    double px=0;
     	    double py=0;
     	    if(scrollX!=0 && prevDimension!=null){//prevDimension sometime null happen.
     	        px=(double)scrollX/(prevDimension.width-lws.getClientArea().width);
     	        double newP=(scalableLayeredPane.getSize().width-lws.getClientArea().width)*px;
     	        lws.scrollToX((int)newP);//move Center???
     	    	}
     	    
     	    if(scrollY!=0 && prevDimension!=null){
     	        py=(double)scrollY/(prevDimension.height-lws.getClientArea().height);
     	        double newP=(scalableLayeredPane.getSize().height-lws.getClientArea().height)*py;
     	        lws.scrollToY((int)newP);//move Center???
     	    	}
     	    
     	    //lws.scrollToX((int)(scrollX*wchanged));//scrollToY;
     	    
     	   // lws.getVerticalBar().setSelection();
     	    
     	    //log.info("changeX:"+lws.getHorizontalBar().getSelection());
     	    //log.info("x-max:"+lws.getHorizontalBar().getMaximum());
     	    //log.info("changeY:"+lws.getVerticalBar().getSelection());
     	    //log.info("y-max:"+lws.getVerticalBar().getMaximum());
             }
         }
 	    
 	}

}
